import os
import re
import time
import urllib
import urllib.request

from bs4 import BeautifulSoup


# 获取该网页的源代码
def getPage(url):
    try:
        user_agent = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36'
        header = {'User-Agent': user_agent}
        request = urllib.request.Request(url, headers=header)
        response = urllib.request.urlopen(request)
        pageCode = response.read().decode('utf-8')
        return pageCode
    except Exception as e:
        print('获取网页失败：')
        print(e)


# 将双色球的信息写入txt文件
def writePage(date, dateNum, num):
    # 日期的列表会比期号长1，因为最后加入了一组页数统计，所以不能以日期列表长度来写数据
    with open('DrawNumber.txt', 'a+') as f:
        for x in range(0, len(num)):
            f.write('(' + date[x] + ';' + dateNum[x] + ';' + num[x] + ')' + '\n')


# 获取总页数
def getPageNum(url):
    try:
        pageCode = getPage(url)
        soup = BeautifulSoup(pageCode, 'html.parser')
        td_code = soup.find('td', colspan='7')  # 获取表格中包含页数的列
        result = td_code.get_text().split(' ')  # 将该列转换成列表
        # result = ['\n', '共125', '页', '/2484', '条记录', '首页', '上一页', '下一页', '末页', '当前第', '2', '页']
        # 用正则表达式从reslut[1]中提取出数字
        list_num = re.findall("[0-9]{1}", result[1])
        # 将提取出来的数字转换成十进制的数值
        page_num = int(list_num[0] + list_num[1] + list_num[2])
        return page_num
    except Exception as e:
        print('获取总页数失败：')
        print(e)


def getPage1(href):
    while True:
        try:
            time.sleep(1)
            page = BeautifulSoup(getPage(href), 'html.parser')
            em_list = page.find_all('em')  # 获取该页面中的em内容,即中奖编号所在
            print(em_list)
            if em_list is not None:
                return page
        except Exception as f:
            print(f)
            continue


# 获取单页中的双色球中奖信息
def getDrawNumber(url):
    # 循环读取每一页的信息
    for num in range(1, getPageNum(url) + 1):
        try:
            href = 'http://kaijiang.zhcw.com/zhcw/html/ssq/list_' + str(num) + '.html'
            print('获取第{}页数据，url {}'.format(str(num), href))
            # page = BeautifulSoup(getPage(href), 'html.parser')
            page = getPage1(href)
            em_list = page.find_all('em')  # 获取该页面中的em内容,即中奖编号所在
            td_list = page.find_all('td', {'align': 'center'})  # 获取该页面中的开奖日期，期号等信息

            i = 1  # 计数器，每七个号码为一组
            Drawnum = ''  # 存放一期中奖号码
            Drawnum_list = []  # 存放该页每期的中奖号码
            for em in em_list:
                emnum = em.get_text()
                if i == 7:
                    Drawnum = Drawnum + emnum
                    Drawnum_list.append(Drawnum)
                    Drawnum = ''  # 重置一期的号码
                    i = 1  # 重置计数器
                else:
                    Drawnum = Drawnum + emnum + ','
                    i += 1

            Drawdate = []  # 存放开奖日期
            DrawdateN = []  # 存放期号
            t = 1  # 计数器，每5个为一组，我们只需要每组的前两个td内容
            for td in td_list:
                td_text = td.get_text()
                if t == 1:
                    Drawdate.append(td_text)
                    t += 1
                elif t == 2:
                    DrawdateN.append(td_text)
                    t += 1
                elif t == 5:
                    t = 1
                else:
                    t += 1
            writePage(Drawdate, DrawdateN, Drawnum_list)
        except Exception as e:
            continue


# 获取最新count期的双色球中奖信息
def getNewestDrawNumber(url, count):
    # 循环读取第一页的信息
    try:
        page = BeautifulSoup(getPage(url), 'html.parser')
        em_list = page.find_all('em')  # 获取该页面中的em内容,即中奖编号所在
        td_list = page.find_all('td', {'align': 'center'})  # 获取该页面中的开奖日期，期号等信息

        i = 1  # 计数器，每七个号码为一组
        j = 1  # 计数器，获取最新几期号码
        Drawnum = ''  # 存放一期中奖号码
        Drawnum_list = []  # 存放该页每期的中奖号码
        for em in em_list:
            if j > count:
                break
            emnum = em.get_text()
            if i == 7:
                Drawnum = Drawnum + emnum
                Drawnum_list.append(Drawnum)
                Drawnum = ''
                i = 1
                j += 1
            else:
                Drawnum = Drawnum + emnum + ','
                i += 1
        Drawdate = []  # 存放开奖日期
        DrawdateN = []  # 存放期号
        t = 1  # 计数器，每5个为一组，我们只需要每组的前两个td内容
        tj = 1
        for td in td_list:
            if tj > count:
                break
            td_text = td.get_text()
            if t == 1:
                Drawdate.append(td_text)
                t += 1
            elif t == 2:
                DrawdateN.append(td_text)
                t += 1
            elif t == 5:
                t = 1
                tj += 1
            else:
                t += 1
        for x in range(0, len(Drawnum_list)):
            print('(' + Drawdate[x] + ';' + DrawdateN[x] + ';' + Drawnum_list[x] + ')')
    except Exception as e:
        print(e)


if __name__ == '__main__':
    url = 'http://kaijiang.zhcw.com/zhcw/html/ssq/list_1.html'
    flag = 1  # 1 获取所有开奖结果保存文件中  2获取最新count期开奖结果打印到控制台
    if flag == 1:
        if os.path.exists("DrawNumber.txt"):
            os.remove("DrawNumber.txt")
        getDrawNumber(url)
    if flag == 2:
        count = 3
        getNewestDrawNumber(url, count)
